<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>社保</span>
        </template>
        <template #right>
          <el-button v-allow="'import_excel'" type="warning" size="small" @click="$router.push('/import')">导入excel</el-button>
          <el-button v-allow="'export_excel'" type="danger" size="small" @click="excel">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
    </div>

    <el-card style="margin-top: 10px" :default-sort="{prop:'workNumber'}">
      <el-table border :data="list">
        <el-table-column label="序号" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto">
          <template slot-scope="scope">
            <ImgHolder :src="scope.row.staffPhoto" />
          </template>
        </el-table-column>
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template slot-scope="scope">
            {{ format(scope.row.formOfEmployment) }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column sortable label="入职时间" prop="correctionTime" />
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)">查看</el-button>
            <el-button type="text" size="small" @click.native="hAssign(scope.row.id)">分配角色</el-button>
            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="total, sizes,prev, pager, next, jumper"
          :total="params.total"
          :page-size="params.size"
          :page-sizes="[2,5,10]"
          :current-page="params.page"
          @current-change="hCurrentChange"
          @size-change="hSizeChange"
        />
      </el-row>
    </el-card>
    <!-- 子组件 -->
    <el-dialog :visible.sync="showDialog" title="新增员工" @close="cDialog">
      <emp-dialog ref="Dialog" @update-empolyee="hEmpolyee" @closes="showDialog=false" />
    </el-dialog>

    <!-- 使用组件 -->
    <el-dialog
      title="分配角色"
      :visible.sync="showDialogRole"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AssignRole :id="id" ref="assignRole" @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
// import PageTools from '@/components/PageTools/index.vue'
import AssignRole from './AssignRole' // 引入组件
import { hireType } from '@/constant/employees'
import empDialog from './empDialog.vue'
export default {
  components: { empDialog, AssignRole },
  data() {
    return {
      showDialogRole: false,
      list: [], // 员工列表
      params: {
        page: 1,
        total: 0, // 数据总条数
        size: 2
      },
      id: '', // 根据id分配角色
      showDialog: false
    }
  },

  created() {
    this.loadEmployee()
  },
  // 获取员工列表
  methods: {
    async loadEmployee() {
      const res = await getEmployeeList(this.params)
      console.log('获取员工列表', res)
      this.list = res.data.rows
      this.params.total = res.data.total
    },
    format(data) {
      return hireType[data]
    },
    hCurrentChange(val) {
      this.params.page = val
      this.loadEmployee()
    },
    hSizeChange(val) {
      this.params.size = val
      this.loadEmployee()
    },
    // 点击删除
    del(id) {
      this.$confirm('确定删除吗?', '系统提示').then(() => {
        this.doDel(id)
      }).catch(() => {})
    },
    async doDel(id) {
      // 发送请求
      await delEmployee(id)
      // 判断如果当前删除的是表格中的最后一条数据
      if (this.list.length === 1 && this.params.page > 1) {
        this.params.page--
      }
      // 重新渲染
      this.loadEmployee()
      // 提示消息
      this.$message.success('删除成功')
      console.log('删除')
    },
    // 子组件通知父组件关闭
    // 接收子组件传来的,重新渲染页面
    hEmpolyee() {
      this.params.total++ // 添加成功,进入最后一页看数据
      this.params.page = Math.ceil(this.params.total / this.params.size)
      console.log(this.params.page)
      this.loadEmployee()
      this.showDialog = false
    },
    // 关闭对话框,清空子组件表单
    cDialog() {
      this.$refs.Dialog.resetForm()
    },
    formatData(rows) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      // console.log(rows)
      // 转成中文
      const header = Object.keys(rows[0]).map(item => { return map[item] })
      const data = rows.map(item => {
        const key = item['formOfEmployment'] // 1,2
        item['formOfEmployment'] = hireType[key]
        return Object.values(item)
      })
      return { header, data }
    },
    excel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getEmployeeList(this.params)
        const list = res.data.rows
        console.log('从后端获取的数据', list)
        const { header, data } = this.formatData(list)
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 分配角色
    hAssign(id) {
      this.showDialogRole = true
      this.id = id
      this.$nextTick(() => {
        this.$refs.assignRole.loadRoles()
      })
    },
    // 子组件通知关闭碳层
    hClose() {
      this.showDialogRole = false
    }
    // zuoye(total, size, page, btnCount = 5) {
    //   const arr = []
    //   if (page < 4) {
    //     // eslint-disable-next-line no-const-assign
    //     arr = [1, 2, 3, 4, 5]
    //   }else {

    //   }
    //   return arr
    // }

  }
}

</script>
